//依赖jQuery
define(['template','jquery'],(template)=>{
	class Header{
		constructor(arg) {
		    this.loadHTML().then(()=>{
				this.languageSwitch()
				this.getTabs()
				this.getNavData()
				this.getMenulist()
				this.languagechange()
				this.cartCount()
			})
			
		}
		loadHTML(){
			return new Promise((reslove)=>{
				$('header').load('/html/modules/header.html',()=>{
					reslove()
				})
			})
			//使用jQuery的load方法加载header.html
			//绝对路径
		}
		languageSwitch(){
			// console.log($('.languagemeun'))
			$('.languagemeun').on('click',function(){
				$('.headermian-top-right language').addClass('small')
				alert(1)
				// console.log()
			})
		}
		
// 获取nav数据
		getTabs(){
			$.get('http://www.xiongmaoyouxuan.com/api/tabs',resp=>{
				// console.log(resp)
				if(resp.code===200){
					const {list} = resp.data
					$('#tabList').html(template('tabListTemplate',{
						list:list.slice(1,9)
						}))
				}
				
			})
		}
//
		getNavData(){
			$('')
		}
		getMenulist(){
			$('#tabList').on("mouseenter mouseleave","li",function(event){
			 　　if(event.type == "mouseenter"){
						$('#menu').show()
			　　　　 	let id=$(this).attr("li-id")
						$.get(`http://www.xiongmaoyouxuan.com/api/tab/${id}`,{start:0},resp=>{
							// console.log(resp)
							if(resp.code===200){
								const {categories,category}=resp.data
								const html=template('menulistTemplate',{categories,category})
								// console.log(html)
								$('#menulist').html(html)
							}
						})
			 　　}else if(event.type == "mouseleave"){
			 　　　　//mouseleave
						$('#menu').hide()
			 　　};
			 });
			 
			 // 离开menu隐藏
			 $('#menu').on("mouseenter mouseleave",function(event){
			  　　if(event.type == "mouseenter"){
			 			$('#menu').show()
			 　　　　 
			  　　}else if(event.type == "mouseleave"){
			  　　　　//mouseleave
			 			$('#menu').hide()
			  　　};
			  });
		}
		languagechange(){
			$('#language').on("mouseenter mouseleave",function(event){
			 　　if(event.type == "mouseenter"){
						$('#languagemeun2').show()
			 　　}else if(event.type == "mouseleave"){
			 　　　　//mouseleave
						$('#languagemeun2').hide()
						$('#language').addClass('small')
			 　　};
			 });
		}
		cartCount(){
			let count=0
			let cart =localStorage.getItem('cart')
			if(cart){
				cart =JSON.parse(cart)
				count =cart.reduce((num,shop)=>{
					return num + shop.count
				},0)
				$('#listnumber').html(count)
				$('#cartnumber').html(count)
			}
		}
	}
	return new Header()
})
